import Api from '../../constants/Api'

export default {
  namespaced: true,
  state: {
    userList: [],
    userDown: [],
    dataPmsTree: []
  },
  mutations: {
    userListTo(state, data) {
      state.userList = data
    },
    userDownTo(state, data) {
      state.userDown = data
    },
    dataPmsTreeTo(state, data) {
      state.dataPmsTree = data
    }
  },
  actions: {
    getUserList({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.getUserList,
        data: {
          current: that.current,
          pageSize: that.pageSize,
          ...that.searchForm
         
        }
      }).then(res => {
        commit('userListTo', res.data.records)
        that.total = res.data.total
        that.tableLoading = false
      }).catch(() => {
        that.tableLoading = false
      })
    },
    getUserDown({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.getUserList,
        data: {
          deptId: that.deptId 
        }
      }).then(res => {
        commit('userDownTo', res.data)
      }).catch(() => {
      })
    },
    addUser({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.addUser,
        data: that.userForm
      }).then(res => {
        if (res.code === 20002) {
          that.handleVisible = false
          that.$message.success(res.msg)
          that.current = 1
          that.getData()
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    },
    editUser({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.editUser,
        data: that.userForm
      }).then(res => {
        if (res.code === 20003) {
          that.handleVisible = false
          that.$message.success(res.msg)
          that.getData()
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    },
    removeUser({state}, that) {
      let url = Api.removeUser.replace('{userId}', that.userId)
      return that.$axios({
        method: 'delete',
        url: url
      }).then(res => {
        if (res.code === 20004) {
          that.delVisible = false
          that.getData()
          that.$message.success(res.msg)
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    },
    modPassword({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.modPassword,
        data: that.modForm
      }).then(res => {
        if (res.code === 20003) {
          that.modVisible = false
          that.$message.success(res.msg)
          that.getData()
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    },
    resetPassword({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.resetPassword,
        data: that.modForm
      }).then(res => {
        if (res.code === 20003) {
          that.modVisible = false
          that.$message.success(res.msg)
          that.getData()
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    },
    getDataPms({commit}, that) {
      that.$axios({
        method: 'get',
        url: Api.getDataPms,
        params: {
          userId: that.$global.userInfo.userId
        }
      }).then(res => {
        commit('dataPmsTreeTo', res.data)
      }).catch(() => {
      })
    },
    getUserDataIds({commit}, that) {
      return that.$axios({
        method: 'get',
        url: Api.getUserDataIds,
        params: {
          userId: that.userId
        }
      }).then(res => {
        return res.data
      }).catch(() => {
      })
    },
    addDataPms({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.addDataPms,
        data: that.dataPmsForm
      }).then(res => {
        if (res.code === 20002) {
          that.pmsShow = false
          that.$message.success(res.msg)
          that.getData()
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    }
  }
}
